package com.nari.ies.ne.nw.server.service.fileDataCheck.impl;

import com.nari.ies.ne.nw.server.entity.FileDataCheckEntity;
import com.nari.ies.ne.nw.server.mapper.FileDataCheckMapper;
import com.nari.ies.ne.nw.server.mapper.PlantDataMapper;
import com.nari.ies.ne.nw.server.service.fileDataCheck.ICDQYCFileDataCheckService;
import com.nari.ies.ne.nw.server.util.DataUtil;
import com.nari.ies.ne.nw.server.util.IoUtil;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class CDQYCFileDataCheckServiceImpl implements ICDQYCFileDataCheckService {
    @Autowired
    private PlantDataMapper plantDataMapper;

    @Autowired
    private FileDataCheckMapper fileDataCheckMapper;

    @Override
    public void FDCDQFileDataCheck(File file) {
//        System.out.println("数据评分--------------------" + file.getName());
        log.info("数据评分--------------------" + file.getName());
        FileDataCheckEntity fileDataCheckEntity = new FileDataCheckEntity();
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId ;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型
        int powerNoData = 0;//功率无数据
        int powerQualifiedData = 0;//功率合格数据(有数据且合格)
        int powerUnqualifiedData = 0;//功率不合格数据(有数据但不合格)
        int capacityNoData = 0;//容量无数据
        int capacityQualifiedData = 0;//容量合格数据(有数据且合格)
        int capacityUnqualifiedData = 0;//容量不合格数据(有数据但不合格)

        String tableName = "HISDB.NW_NE_FILE_DATACHECK_FD_CDQYC";//表名
        // 数据计数
        int count = 0;

        // 文件名称 FD_GD.GDZJFSFD_CDQYC_20230612_073000.dat
        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 读取到的每一行数据
            String line = "";

            while ((line = br.readLine()) != null) {
                // 预测时间标签行
                if (line.contains("<CDQYC")) {
                    forecastDate = DataUtil.queryTime(line);
                }
                String[] strs = line.split("\t");

                // 数据行
                if (line.contains("#")) {
                    // 数据计数
                    count++;

                    // 功率
                    float power = -1.0f;
                    if (!" ".equals(strs[2]) && !"null".equals(strs[2]) && !"NULL".equals(strs[2])) {
                        power = Float.parseFloat(strs[2]);
                    }

                    // 容量
                    float capacity = 0.0f;
                    if (!" ".equals(strs[3]) && !"null".equals(strs[3]) && !"NULL".equals(strs[3])) {
                        capacity = Float.parseFloat(strs[3]);
                    }

                    // 判断功率是否合格
                    if (power >= 0 && power <= capacity) {
                        // 功率合格
                        powerQualifiedData++;
                    }
                }
            }

            // 功率
            dataType = "power";
            // 功率无数据
            powerNoData = 16 - count;
            // 功率不合格
            powerUnqualifiedData = count - powerQualifiedData;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId,forecastDate,createDate,plantType,fileType,dataType,powerNoData,powerQualifiedData,powerUnqualifiedData);
            dataList.add(fileDataCheckEntity);

            // 容量
            dataType = "capacity";
            // 容量无数据
            capacityNoData = 16 - count;
            // 容量合格
            capacityQualifiedData = count;

            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId,forecastDate,createDate,plantType,fileType,dataType,capacityNoData,capacityQualifiedData,capacityUnqualifiedData);
            dataList.add(fileDataCheckEntity);



            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate,tableName);
            if (data == 0) {
                log.info("数据评分插入-------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList,tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }

    @Override
    public void GFCDQFileDataCheck(File file) {
        log.info("数据评分--------------------"+file.getName());
        FileDataCheckEntity fileDataCheckEntity ;
        List<FileDataCheckEntity> dataList = new ArrayList<>();
        String plantId ;//场站id
        Date forecastDate = null;//预测时间
        Date createDate = new Date();//创建时间
        String plantType;//场站类型
        String fileType;//文件类型
        String dataType;//字段类型

        // 功率预测无数据
        int powerNoData = 0;
        // 功率预测有数据且不合格
        int powerQualifiedNo = 0;
        // 功率预测有数据且合格
        int powerQualifiedData = 0;
        // 机组容量无数据
        int capacityNoData = 0;
        // 机组容量有数据且不合格
        int capacityQualifiedNo = 0;
        // 机组容量有数据且合格
        int capacityQualifiedData = 0;

        String tableName = "HISDB.NW_NE_FILE_DATACHECK_GF_CDQYC";//表名
        // 数据计数
        int count = 0;

        // 文件名称 FD_GD.GDZJFSFD_CDQYC_20230612_073000.dat
        String fileName = file.getName();

        String[] fileNameArr = fileName.split("_");
        // 文件类型
        fileType = fileNameArr[2];
        // 场站类型
        plantType = fileNameArr[0];
        // 场站标识
        String plantCode = fileNameArr[1].split("\\.")[1];

        //String nwCode = fileNameArr[0] + "_" + fileNameArr[1].split("\\.")[0] + ".";
        List<Map<String, Object>> idList = plantDataMapper.selectPlantData(plantCode);

        plantId = idList.get(0).get("ID").toString();
        if (plantId.contains("-")) {
            plantId = plantId.split("-")[0];
        }

        FileReader fr = null;
        BufferedReader br = null;

        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            // 读取到的每一行数据
            String line = "";

            while((line=br.readLine())!=null){
                // 预测时间标签行
                if (line.contains("<CDQYC")) {
                    forecastDate = DataUtil.queryTime(line);
                }
                String[] strs = line.split("\t");

                // 数据行
                if (line.contains("#")) {
                    // 数据计数
                    count++;

                    // 功率
                    if(!" ".equals(strs[2]) && !"null".equals(strs[2]) && !"NULL".equals(strs[2])){
                        if(Float.parseFloat(strs[2]) >=0 ){
                            powerQualifiedData++;
                        }
                    }
                    // 容量
                    if(!" ".equals(strs[3]) && !"null".equals(strs[3]) && !"NULL".equals(strs[3])){
                        capacityQualifiedData++;
                    }
                }
            }

            // 功率
            dataType = "power";
            // 功率无数据
            powerNoData = 16 - count;
            // 功率不合格
            powerQualifiedNo = count - powerQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId,forecastDate,createDate,
                    plantType,fileType,dataType,powerNoData,powerQualifiedData,powerQualifiedNo);
            dataList.add(fileDataCheckEntity);


            // 容量
            dataType = "capacity";
            // 容量无数据
            capacityNoData = 16 - count;
            // 容量不合格
            capacityQualifiedNo = count-capacityQualifiedData;
            fileDataCheckEntity = DataUtil.getFileDataCheckEntity(plantId,forecastDate,createDate,
                    plantType,fileType,dataType,capacityNoData,capacityQualifiedData,capacityQualifiedNo);
            dataList.add(fileDataCheckEntity);

            int data = fileDataCheckMapper.selectFileDataCheck(plantId, forecastDate, tableName);
            if (data == 0) {
                log.info("数据评分插入-------------"+fileName);
                fileDataCheckMapper.insertFileDataCheck(dataList,tableName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(br);
            IoUtil.close(fr);
        }
    }

}
